package com.xk.meeting.utils;

import com.google.gson.Gson;
import com.xk.meeting.entityutils.DateTime;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class UtilHelper {


	/**
	 * 获取当前日期时间
	 * @return 返回当前时间的字符串形式(如"2020-11-12 11:04:00")
	 */
	public static String getTime() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);

	}

	/**
	 * 把date对象转换成String
	 * @param date
	 * @return 返回给定时间的字符串形式(如"2020-11-12 11:04:00")
	 */
	public static String dateToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	/**
	 *
	 * @param num
	 * @return
	 */
	public static DateTime createDateTime(int num, HttpServletRequest request){
		LocalDate ld = LocalDate.now();
		int week = ld.getDayOfWeek().getValue();

		LocalDate date = ld.plusWeeks(num);

		LocalDate monday = date.plusDays(-(week-1));
		LocalDate tuesday = monday.plusDays(1);
		LocalDate wednesday = tuesday.plusDays(1);
		LocalDate thursday = wednesday.plusDays(1);
		LocalDate friday = thursday.plusDays(1);
		LocalDate saturday = friday.plusDays(1);
		LocalDate sunday = saturday.plusDays(1);

		DateTime dateTime = new DateTime();
		dateTime.setMonday(monday.toString());
		dateTime.setTuesday(tuesday.toString());
		dateTime.setWednesday(wednesday.toString());
		dateTime.setThursday(thursday.toString());
		dateTime.setFriday(friday.toString());
		dateTime.setSaturday(saturday.toString());
		dateTime.setSunday(sunday.toString());

		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
		String first = monday.format(formatter);
		String last = sunday.format(formatter);
		dateTime.setDate(first+"—"+last);

		HttpSession session = request.getSession();
		session.setAttribute("datetime",dateTime);

		/*System.out.println(dateTime);*/


		/*Gson gson = new Gson();
		String json = gson.toJson(dateTime);
		session.setAttribute("datetimeJson",json);*/


		return dateTime;
	}

	public static boolean contains(String str, List<String> lists){
		for (String string : lists){
			if (str.contains(string)){
				return true;
			}
		}
		return false;
	}
	

}
